package domain.cms;

import application.util.AppUtils;
import domain.AbstractEntity;
import org.hibernate.annotations.Cascade;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Meta attributes of all pages.
 */
@Entity
@Indexed
public class CmsAttribute extends AbstractEntity<CmsAttribute> implements Comparable<CmsAttribute> {
    @Field(index = Index.TOKENIZED, store = Store.YES)
    private String title = "New-" + AppUtils.randomString(5, 5);
    @Field(index = Index.TOKENIZED, store = Store.YES)
    private String keywords = "";
    @Field(index = Index.TOKENIZED, store = Store.YES)
    private String description = "";
    @Temporal(javax.persistence.TemporalType.DATE)
    @Field
    private Date lastEditedDate;
    @Field(index = Index.TOKENIZED, store = Store.YES)
    @Column(name = "obsah", length = 200000)
    private String content = "";
    @Field(index = Index.TOKENIZED, store = Store.YES)
    @Column(name = "shortContent", length = 500)
    private String shortContent = "";
    private String linkName = "";
    boolean published = true;
    @Column(name = "ORDER_POSITION")
    Integer orderPosition = 0;
    @OneToMany(fetch = FetchType.LAZY)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    List<CmsUrl> oldUrls = new ArrayList<CmsUrl>();
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    @ManyToOne(fetch = FetchType.LAZY)


    CmsUrl url = new CmsUrl();

    public CmsUrl addOldCmsUrl(String url) {
        final CmsUrl url1 = new CmsUrl(url);
        oldUrls.add(url1);
        return url1;
    }

    public void removeOldCmsUrl(String url) throws Exception {
        for (CmsUrl oldUrl : oldUrls) {
            if (oldUrl.getUrl().equals(url)) {
                oldUrls.remove(oldUrl);
                return;
            }

        }
        throw new Exception("url not found");

    }

    public CmsAttribute() {
        lastEditedDate = Calendar.getInstance().getTime();
    }

    public String getLinkName() {
        return linkName;
    }

    public void setLinkName(String linkName) {
        this.linkName = linkName;
    }

    public List<CmsUrl> getOldUrls() {
        return oldUrls;
    }

    public void setOldUrls(List<CmsUrl> oldUrls) {
        this.oldUrls = oldUrls;
    }

    public CmsUrl getUrlAsCmsUrl() {
        if (this.url == null) {
            this.url = new CmsUrl();
        }
        return url;
    }

    public String getUrl() {
        if (this.url == null) {
            this.url = new CmsUrl();
        }
        return url.getUrl();
    }

    public void setUrl(String url) {
        if (this.url == null) {
            this.url = new CmsUrl();
        }
        this.url.setUrl(url.toLowerCase());
    }

    public void setUrl(CmsUrl url) {
        this.url = url;
    }

    public Integer getOrderPosition() {
        return orderPosition;
    }

    public void setOrderPosition(Integer orderPosition) {
        this.orderPosition = orderPosition;
    }

    public boolean isPublished() {
        return published;
    }

    public void setPublished(boolean published) {
        this.published = published;
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getShortContent() {
        return shortContent;
    }

    public void setShortContent(String shortContent) {
        this.shortContent = shortContent;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public Date getLastEditedDate() {
        if (lastEditedDate != null) {
            return new Date(lastEditedDate.getTime());
        } else {
            return null;
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int compareTo(CmsAttribute o) {
        int lastCmp = orderPosition.compareTo(o.orderPosition);
        if (lastCmp == 0) {
            return title.compareTo(o.title);
        }
        return lastCmp;
    }

    @Override
    public boolean equals(Object o) {


        return true;
    }

    @Override
    public int hashCode() {
        int result = title != null ? title.hashCode() : 0;
        result = 31 * result + (keywords != null ? keywords.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (lastEditedDate != null ? lastEditedDate.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (shortContent != null ? shortContent.hashCode() : 0);
        result = 31 * result + (published ? 1 : 0);
        result = 31 * result + (orderPosition != null ? orderPosition.hashCode() : 0);
        return result;
    }
}
